package com.wnxy.admin.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.dto.JChargingCardDto;
import com.github.pagehelper.PageInfo;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.dto.JChargingCardDto;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.Map;

import java.time.LocalDateTime;
import java.util.Arrays;

import java.math.BigInteger;
import java.util.Date;
import java.util.Map;

import com.wnxy.admin.entity.ChargingCard;
import com.wnxy.admin.service.IChargingCardService;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 充电卡 前端控制器
 * </p>
 *
 * @author SJH
 * @since 2023-12-04
 */
@CrossOrigin
@RestController
@RequestMapping("/charging-card")
public class ChargingCardController {

    private final IChargingCardService chargingCardServiceImpl;
    public ChargingCardController(IChargingCardService chargingCardServiceImpl){
        this.chargingCardServiceImpl = chargingCardServiceImpl;
    }

    @PostMapping("/findByPage")
    public ResponseResult<PageInfo<JChargingCardDto>> findByPage(@RequestBody Map<String,Object> params){
        String name = (String) params.get("name");
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        //2.调用业务层
        PageInfo<JChargingCardDto> pageInfo = chargingCardServiceImpl.findByPage(name,pageNum,pageSize);
        //3.返回结果
        return ResponseResult.ok(pageInfo);
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody ChargingCard chargingCard){
        chargingCard.setCardOpeningTime(LocalDateTime.now());
        chargingCard.setChargingStationId(1L);
        chargingCard.setChargingCardState(1);
        chargingCardServiceImpl.save(chargingCard);
        return ResponseResult.ok();
    }

    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody ChargingCard chargingCard){
        boolean b = chargingCardServiceImpl.updateById(chargingCard);
        return ResponseResult.ok(b);
    }

    @GetMapping("/delete")
    public ResponseResult delete(Long id){
        chargingCardServiceImpl.removeById(id);
        return ResponseResult.ok();
    }

    @PostMapping("/deleteBatch")
    public ResponseResult deleteBatch(@RequestBody String[] ids){
        chargingCardServiceImpl.removeBatchByIds(Arrays.asList(ids));
        return ResponseResult.ok();
    }

    //查询充值卡有没有用户，判断一下状态接口
    @PostMapping("/queryByChargingCard/{customerId}")
    public ResponseResult<Boolean> queryByChargingCard(@PathVariable Long customerId){
        LambdaQueryWrapper<ChargingCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChargingCard::getCustomerId,customerId)
                        .eq(ChargingCard::getChargingCardState,1);
        boolean b = chargingCardServiceImpl.count(wrapper) > 0;
        return ResponseResult.ok(b);
    }

    /**
     * 禁用充电卡
     */
    @PostMapping("/disabled")
    public ResponseResult disabledChargingCard(Long id){
        LambdaUpdateWrapper<ChargingCard> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ChargingCard::getId,id).set(ChargingCard::getChargingCardState,2);
        chargingCardServiceImpl.update(wrapper);
        return ResponseResult.ok();
    }

}
